package ustc.browser;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Tab{
	
	private WebView  mWebView = null;
	private	String	 mTitle = null;
	private	String	 mUrl = null;
	private Bitmap	 mFaviIcon = null;
	private Activity mActivity = null;
	
	public	Tab(Activity act){
		mActivity = act;
		mWebView = new WebView(mActivity);
		mWebView.getSettings().setJavaScriptEnabled(true);
		//Set the WebChromeCient and WebViewClient
		//for the web view.
		setClient(mWebView);
	}
	
	//Getters
	public WebView getWebView(){
		return	mWebView;
	}
	public String getTitle(){
		return	mTitle;
	}
	public String getUrl(){
		return	mUrl;
	}
	public Bitmap getFaviIcon(){
		return mFaviIcon;
	}
	
	//Setters
	public void setTitle(String title){
		mTitle = title;
	}
	public void setUrl(String url){
		mUrl = url;
	}
	public void setFaviIcon(Bitmap icon){
		mFaviIcon = icon;
	}
	
	public void loadUrl(String url){
		getWebView().loadUrl(url);
	}
	
	private void setClient(WebView wv){
	//Desc: Set the WebChromeClient and the WebViewClient for
	//	the corresponding WebView.
		wv.setWebChromeClient(new WebChromeClient(){
			public void onReceivedIcon(WebView view, Bitmap icon){
				setFaviIcon(icon);
				BitmapDrawable bd = new BitmapDrawable(getFaviIcon());
				mActivity.setFeatureDrawable(Window.FEATURE_LEFT_ICON, bd);
				((BrowserActivity)mActivity).onWebDataReceived();
			}
			
			public void onReceivedTitle (WebView view, String title){
				setTitle(title);
				mActivity.setTitle(getTitle());
				((BrowserActivity)mActivity).onWebDataReceived();
			}
			
			public void onProgressChanged(WebView view, int progress){
			//Progress of Activity and WebView has 
			//different measurement, so we multiplied 1000
				mActivity.setProgress(progress*1000);
			}
		});
			
		wv.setWebViewClient(new WebViewClient(){
		//Handling related connection errors.
			public void onReceivedError (WebView view, int errorCode,
					String description, String failingUrl){
				switch(errorCode){
				case	ERROR_AUTHENTICATION:
					Toast.makeText(mActivity, "Error: "+description, 
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_BAD_URL:
					Toast.makeText(mActivity, "Error: "+description, 
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_CONNECT:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_FAILED_SSL_HANDSHAKE:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_FILE:
					Toast.makeText(mActivity, "Error: "+description, 
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_FILE_NOT_FOUND:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_HOST_LOOKUP:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_IO:
					Toast.makeText(mActivity, "Error: "+description, 
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_PROXY_AUTHENTICATION:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_REDIRECT_LOOP:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_TIMEOUT:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_TOO_MANY_REQUESTS:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_UNKNOWN:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_UNSUPPORTED_AUTH_SCHEME:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				case	ERROR_UNSUPPORTED_SCHEME:
					Toast.makeText(mActivity, "Error: "+description,
							Toast.LENGTH_SHORT).show();
					break;
				}
			} 
			
			//Tell webview to handle url loading itself
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}

			public void onPageFinished(WebView view, String url){
			//Called when the page loading has finished
				mActivity.setProgressBarIndeterminate(false);
				mActivity.setProgressBarVisibility(false);
				setUrl(url);
				((BrowserActivity)mActivity).onWebDataReceived();
				((BrowserActivity)mActivity).setUrlInAddressText(url);
			}
		});
	}
}